// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

using System.Xml.Linq;

namespace System.Activities.Runtime.DurableInstancing;

[Serializable]
public class InstancePersistenceException : Exception
{
    private const string CommandNameName = "instancePersistenceCommandName";

    public InstancePersistenceException()
        : base(ToMessage(null))
    {
    }

    public InstancePersistenceException(string message)
        : base(message)
    {
    }

    public InstancePersistenceException(string message, Exception innerException)
        : base(message, innerException)
    {
    }

    public InstancePersistenceException(XName commandName)
        : this(commandName, ToMessage(commandName))
    {
    }

    public InstancePersistenceException(XName commandName, Exception innerException)
        : this(commandName, ToMessage(commandName), innerException)
    {
    }

    public InstancePersistenceException(XName commandName, string message)
        : base(message)
    {
        CommandName = commandName;
    }

    public InstancePersistenceException(XName commandName, string message, Exception innerException)
        : base(message, innerException)
    {
        CommandName = commandName;
    }

    protected InstancePersistenceException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
        CommandName = info.GetValue(CommandNameName, typeof(XName)) as XName;
    }

    public XName CommandName { get; private set; }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info.AddValue(CommandNameName, CommandName, typeof(XName));
    }

    private static string ToMessage(XName commandName)
    {
        return commandName == null ? SR.GenericInstanceCommandNull : SR.GenericInstanceCommand(commandName);
    }
}
